﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SzCourt.Logic.BLL;
using SzCourt.Logic.NBL.DBEntities;
using KMFrame.Permission.Util;

namespace SzCourt.WebSite.shenwu
{
    public partial class cpview : System.Web.UI.Page
    {
        private string _ID = string.Empty;
        private string _Title = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                getInfo();
            }
            if (SessionBox.CheckUserSession())
            {
                this.login_no.Visible = true;
                this.spUserName.InnerHtml = SessionBox.GetUserSession().UserName;
                this.hdnUserName.Value = this.spUserName.InnerHtml;
            }
            else
            {
                this.login_yes.Visible = true;
            }
        }

        private void getInfo()
        {

            this._ID = string.IsNullOrEmpty(Request.QueryString["id"]) ? "" : Request.QueryString["id"];
            if (this._ID.Length <= 0)
            {
                Response.Redirect("/shenwu/cpws.aspx");
            }
            this.hdnID.Value = this._ID;


            RefereeDoc bll = new RefereeDoc();
            pRefereeDoc result = (pRefereeDoc)bll.GetRefereeDocInfo(Convert.ToInt32(this._ID));

            if (result != null)
            {
                this.HDocLookedUp.InnerHtml = result.DocLookedUp;
                this.pCaseName.InnerHtml = result.CaseName;
                this.WebContent.InnerHtml = result.CaseContent.Replace(" ", "&nbsp;").Replace("\n", "<br/>");
                this._Title = result.CaseName;
            }

            List<pRefereeDocComment> commentList = new List<pRefereeDocComment>();
            try
            {
                commentList = (List<pRefereeDocComment>)bll.GetpCommentList(Convert.ToInt32(this._ID));
                this.rpCommentList.DataSource = commentList;
                this.rpCommentList.DataBind();
            }
            catch
            {

            }
            
            SetPageInfo();
        }

        /// <summary>
        /// 写页面title,keyword,description
        /// </summary>
        /// <param name="mWord"></param>
        private void SetPageInfo()
        {
            //this.TabName.InnerHtml = this._Title;
            Page.Title = string.Format("{0}  - 深圳市中级人民法院", this._Title);
        }


        /// <summary>
        /// 获取IP地址 
        /// </summary>
        /// <returns></returns>
        private string GetClientIP()
        {
            string userHostAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContext.Current.Request.UserHostAddress;
            }
            if (!string.IsNullOrEmpty(userHostAddress))
            {
                return userHostAddress;
            }
            return "127.0.0.1";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string errmsg = "";
            bool isLog = false;
            pRefereeDocComment param = new pRefereeDocComment();
            if (SessionBox.CheckUserSession())
            {
                param.AddUserID = SessionBox.GetUserSession().UserID;
                param.AddUserName = SessionBox.GetUserSession().UserName;               
                isLog = true;
            }
            else
            {
                SzCourt.Logic.BLL.UserInfo userBLL = new SzCourt.Logic.BLL.UserInfo();
                PUserInfo result = (PUserInfo)userBLL.Login(this.lgnUserName.Value, this.lgnPassword.Value);

                if (result != null)
                {
                    UserSession user = new UserSession(result.UserID, result.UserName, result.UserGUID);
                    SessionBox.CreateUserSession(user);

                    param.AddUserID = result.UserID;
                    param.AddUserName = this.lgnUserName.Value;
                    isLog = true;
                }

            }

            if (isLog)
            {
                param.AddIP = GetClientIP();
                param.Conent = this.textConent.Value;
                param.RefereeDocID = Convert.ToInt32(this.hdnID.Value);
                RefereeDoc bll = new RefereeDoc();
                bool result = (bool)bll.AddComment(param);
                if (result)
                {
                    Response.Redirect("/shenwu/cpview.aspx?id=" + this.hdnID.Value);
                }
                else
                {
                    this.sendMessage.InnerHtml = "发表评论失败！";
                }
            }
            else
            {
                this.sendMessage.InnerHtml = "用户名或密码错误！";
            }
        }
    }
}